[% setvar title rindex and index should return true/false values %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>rindex and index should return true/false values</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Nathan Torkington &lt;<a href='mailto:gnat@frii.com'>gnat@frii.com</a>&gt;
  Date: 12 Sep 2000
  Last Modified: 15 Sep 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 213
  Version: 2
  Status: Developing</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>index() and rindex() should return <code>undef</code> if their
second argument is not a substring of their first
argument.  If the substring occurs at the start of
the first argument, they should return &quot;0 but true&quot;.</p>
<a name='CHANGES'></a><h1>CHANGES</h1>
<pre> * added &quot;0 but true&quot; suggestion</pre>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>In perl5, index() and rindex() return -1 if the
substring isn't found.  This seems out of step with
the rest of Perl's functions, which return <code>undef</code>
on error.  I propose changing index() and rindex()
to return <code>undef</code> if the substring isn't found.</p>
<p>This would also cause warnings to be issued when
programmers use the results of index() or rindex()
assuming the substring was found.</p>
<p>This suggestion doesn't rely on RFC 53, &quot;Built-ins: Merge and
generalize <code>index</code> and <code>rindex</code>&quot;, and works regardless
of whether 53 is accepted or not.</p>
<p>When the substring occurs at the start of the larger
string, the functions should return &quot;0 but true&quot;.  This
is exempt from &quot;isn't numeric&quot; warnings, and gives them
useful boolean return values:</p>
<pre>  if (index($big_string, $little_string)) {
    print &quot;$little_string is in $big_string&quot;;
  }</pre>
<p>without breaking their existing numeric uses.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>The perl526 translator could turn index($a,$b) calls into</p>
<pre>  do { my $tmp = index($a,$b);
       if (defined($tmp)) { 0 + $tmp }
       else               { -1       }
  }</pre>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>RFC 53: Built-ins: Merge and generalize <code>index</code> and <code>rindex</code></p>
<p>perlfunc manpage for information on index() and rindex()</p>
</div>
